package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.service.ReportService;
import cn.itcast.service.SetmealService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/report")
public class ReportMemberController {

    @Reference(version = "1.0.0")
    private ReportService reportService;

    @Reference(version = "1.0.0")
    private SetmealService  setmealService;

    /**
     * 会员数量统计
     * @return
     */
    @GetMapping("/member/count")
    public Result findReportMember(){

        //计算过去一年的12个月
        Map<String,Object> map=new HashMap<>();//构建map集合封装响应数据
        Calendar calendar=Calendar.getInstance();//获取当前时间
        List<String> months=new ArrayList<>();
        calendar.add(Calendar.MONTH,-12);//获取12月之前的时间

        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH,1);//当前月向后推一个月
            Date time = calendar.getTime();
            months.add(new SimpleDateFormat("yyyy-MM").format(time));
        }
        map.put("months",months);


        List<Integer> memberCount=this.reportService.findReportMember(months);
        map.put("memberCount",memberCount);


        return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
    }

    /**
     *  套餐预约统计
     * @return
     */
    @GetMapping("/setmeals")
    public Result findReportSetmeal(){
        //构建一个map集合，封装响应数据
        Map<String,Object> map=new HashMap<>();
        try {

            //响应的数据格式{value: 1048, name: '搜索引擎'},
            List<Map<String,Object>> list=this.setmealService.findSetmealOrderedCount();
            map.put("setmealCount",list);
            //遍历集合,取出map的名称存到一个集合中赋值给setmealNames
            List<String> list1=new ArrayList<>();
            for (Map<String, Object> map1 : list) {
                list1.add((String) map1.get("name"));
            }
            map.put("setmealNames",list1);
            return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }
    /**
     * 运营数据统计
     */

    @GetMapping("/business")
    public Result findBusiness(){

        try {
            //查询统计数据
            Map<String,Object> hashMap= this.reportService.findBusiness();
            return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS,hashMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }

    }

    /**
     * 导出运营数据
     * @return
     */
    @GetMapping("/export/business")
    public Result exportExcel(HttpServletRequest request,HttpServletResponse response){
        try {
            Map<String, Object> map = this.reportService.findBusiness();
            String reportDate = (String) map.get("reportDate");
            Integer todayNewMember = (Integer) map.get("todayNewMember");
            Integer totalMember = (Integer) map.get("totalMember");
            Integer thisWeekNewMember = (Integer) map.get("thisWeekNewMember");
            Integer thisMonthNewMember = (Integer) map.get("thisMonthNewMember");
            Integer todayOrderNumber = (Integer) map.get("todayOrderNumber");
            Integer todayVisitsNumber = (Integer) map.get("todayVisitsNumber");
            Integer thisWeekOrderNumber = (Integer) map.get("thisWeekOrderNumber");
            Integer thisWeekVisitsNumber = (Integer) map.get("thisWeekVisitsNumber");
            Integer thisMonthOrderNumber = (Integer) map.get("thisMonthOrderNumber");
            Integer thisMonthVisitsNumber = (Integer) map.get("thisMonthVisitsNumber");
            List<Map> hotSetmeal = (List<Map>) map.get("hotSetmeal");

            String[] name=new String[5];
            String[] setmealcount=new String[5];
            String[] proportion=new String[5];

            for (int i = 0; i < hotSetmeal.size(); i++) {
                name[i]= (String) hotSetmeal.get(i).get("name");
                setmealcount[i]= String.valueOf(hotSetmeal.get(i).get("setmeal_count"));
                proportion[i]= String.valueOf(hotSetmeal.get(i).get("proportion")) ;
            }




            //获取文件的绝对路径
            String filePath= "D:\\develope\\health_xiongdiqixin\\itcast-health-backend\\src\\main\\resources\\static\\template\\report_template.xlsx";
            /*String filePath1= request.getSession().getServletContext().getRealPath("/");
            System.out.println(filePath1);*/

            //创建一个表格对象
            XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(new File(filePath)));
            //获取工作表
            XSSFSheet sheetAt = wb.getSheetAt(0);
            //获取工作表的第三行
            XSSFRow row = sheetAt.getRow(2);
            //获取第三行第六列
            XSSFCell cell = row.getCell(5);
            cell.setCellValue(reportDate);//填充日期
            //获取第五行第六列填充数据
            sheetAt.getRow(4).getCell(5).setCellValue(todayNewMember);
            //获取第五行第八列填充数据
            sheetAt.getRow(4).getCell(7).setCellValue(totalMember);
            //获取第六行第六列填充数据
            sheetAt.getRow(5).getCell(5).setCellValue(thisWeekNewMember);
            //获取第六行第八列填充数据
            sheetAt.getRow(5).getCell(7).setCellValue(thisMonthNewMember);
            //7，5
            sheetAt.getRow(7).getCell(5).setCellValue(todayOrderNumber);
            //7，7
            sheetAt.getRow(7).getCell(7).setCellValue(todayVisitsNumber);
            //8，5
            sheetAt.getRow(8).getCell(5).setCellValue(thisWeekOrderNumber);
            //8，7
            sheetAt.getRow(8).getCell(7).setCellValue(thisWeekVisitsNumber);
            //9，5
            sheetAt.getRow(9).getCell(5).setCellValue(thisMonthOrderNumber);
            //9，7
            sheetAt.getRow(9).getCell(7).setCellValue(thisMonthVisitsNumber);
            //12，4
            sheetAt.getRow(12).getCell(4).setCellValue(name[0]);
            //12，5
            sheetAt.getRow(12).getCell(5).setCellValue(setmealcount[0]);
            //12，6
            sheetAt.getRow(12).getCell(6).setCellValue(proportion[0]);
            //13，4
            sheetAt.getRow(13).getCell(4).setCellValue(name[1]);
            //13，5
            sheetAt.getRow(13).getCell(5).setCellValue(setmealcount[1]);
            //13，6
            sheetAt.getRow(13).getCell(6).setCellValue(proportion[1]);
            //14，4
            sheetAt.getRow(14).getCell(4).setCellValue(name[2]);
            //14，5
            sheetAt.getRow(14).getCell(5).setCellValue(setmealcount[2]);
            //14，6
            sheetAt.getRow(14).getCell(6).setCellValue(proportion[2]);
            //15，4
            sheetAt.getRow(15).getCell(4).setCellValue(name[3]);
            //15，5
            sheetAt.getRow(15).getCell(5).setCellValue(setmealcount[3]);
            //15，6
            sheetAt.getRow(15).getCell(6).setCellValue(proportion[3]);



            //将生成的Excel文件保存到本地
            ServletOutputStream out = response.getOutputStream();
            //设置响应头信息
            response.setHeader("Content-Disposition","attachment;filename=report.xlsx");
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            //将工作薄写入文件输出流中
            wb.write(out);
            //文本文件输出流，释放资源
            out.close();
            wb.close();

            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }

    }


    /**
     * 收入统计
     */

    @GetMapping("/income")
    public Result findIncome(){

        //计算过去一年的12个月
        Map<String,Object> map=new HashMap<>();//构建map集合封装响应数据
        Calendar calendar=Calendar.getInstance();//获取当前时间
        List<String> months=new ArrayList<>();
        calendar.add(Calendar.MONTH,-12);//获取12月之前的时间

        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH,1);//当前月向后推一个月
            Date time = calendar.getTime();
            months.add(new SimpleDateFormat("yyyy-MM").format(time));
        }
        map.put("months",months);


        List<Double> incomeCount=this.reportService.findIncome(months);
        map.put("incomeCount",incomeCount);


        return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);

    }

    /**
     * 预约/支付统计
     */

    @GetMapping("/orderPay")
    public Result findOrder(){

        //计算过去一年的12个月
        Map<String,Object> map=new HashMap<>();//构建map集合封装响应数据
        Calendar calendar=Calendar.getInstance();//获取当前时间
        List<String> months=new ArrayList<>();
        calendar.add(Calendar.MONTH,-12);//获取12月之前的时间

        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH,1);//当前月向后推一个月
            Date time = calendar.getTime();
            months.add(new SimpleDateFormat("yyyy-MM").format(time));
        }
        map.put("months",months);

        //查询每月预约数量
        List<Integer> orderCount=this.reportService.findOrder(months);
        map.put("orderCount",orderCount);
        //调用收入统计的方法
        List<Double> payCount=this.reportService.findIncome(months);
        map.put("payCount",payCount);


        return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);

    }
}
